package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.pojo.Article;
import com.lagou.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description:
 * @Author: zhoushiduo
 * @Date: 2020/12/12 19:41
 */

@Controller
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private ArticleService articleService;

    /**
     * 直接获取页面的分页参数
     * @param request
     * @param response
     * @param session
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request,
                        HttpServletResponse response, HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        String pageNumO = request.getParameter("pageNum");
        if(pageNumO == null || "".equals(pageNumO)){
            modelAndView.addObject("pageInfo","分页参数（pageNum）异常！！！");
            modelAndView.setViewName("client/error");
            return modelAndView;
        }
        Integer pageNum =  Integer.valueOf(pageNumO);
        PageInfo<Article> articles = articleService.findByWherePage(null, pageNum);
        modelAndView.addObject("articles", articles);
        modelAndView.setViewName("client/index");
        return modelAndView;
    }

    /**
     * 公共分页参数统一由拦截器完成处理，业务方法只关注业务处理
     * @param request
     * @param response
     * @param session
     * @return
     */
    @RequestMapping("/listpage")
    public ModelAndView listPage(HttpServletRequest request,
                              HttpServletResponse response, HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<Article> articles = articleService.findByWherePage(null);
        modelAndView.addObject("articles", articles);
        modelAndView.setViewName("client/index");
        return modelAndView;
    }


}
